/*
 * Copyright (C) 2011 PickleMan <admin.aegamesi@gmail.com>
 * 
 * This file is part of Jeie.
 * Jeie is free software and comes with ABSOLUTELY NO WARRANTY.
 * See LICENSE for details.
 */

package org.jeie.gui.panels;

import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import org.jeie.main.EasyImageEditor;
import org.jeie.util.Util;

public class ToolboxPanel extends JPanel implements ActionListener
	{
	private static final long serialVersionUID = -8461699576095094528L;
	public ColorPanel cp;
	public FillPanel fp;
	public LinePanel lp;

	public JToggleButton lineTool;
	public JToggleButton ellipseTool;
	public JToggleButton pencilTool;
	public JToggleButton rectTool;
	public JToggleButton fillTool;
	public JToggleButton eyeDropperTool;
	public JToggleButton sprayTool;
	public JToggleButton zoomTool;
	public JButton undoButton;
	public JButton redoButton;

	public ToolboxPanel()
		{
		setBackground(Color.lightGray);
		setLayout(null);

		cp = new ColorPanel();
		cp.setBounds(new Rectangle(0,0,48,32));
		add(cp);

		ButtonGroup toolGroup = new ButtonGroup();
		lineTool = new JToggleButton(Util.getIconForKey("ToolboxPanel.LINE")); //$NON-NLS-1$
		ellipseTool = new JToggleButton(Util.getIconForKey("ToolboxPanel.ELLIPSE")); //$NON-NLS-1$
		pencilTool = new JToggleButton(Util.getIconForKey("ToolboxPanel.PENCIL")); //$NON-NLS-1$
		rectTool = new JToggleButton(Util.getIconForKey("ToolboxPanel.RECT")); //$NON-NLS-1$
		fillTool = new JToggleButton(Util.getIconForKey("ToolboxPanel.FILL")); //$NON-NLS-1$
		eyeDropperTool = new JToggleButton(Util.getIconForKey("ToolboxPanel.EYEDROPPER")); //$NON-NLS-1$
		sprayTool = new JToggleButton(Util.getIconForKey("ToolboxPanel.SPRAY")); //$NON-NLS-1$
		zoomTool = new JToggleButton(Util.getIconForKey("ToolboxPanel.ZOOM")); //$NON-NLS-1$
		undoButton = new JButton(Util.getIconForKey("ToolboxPanel.UNDO")); //$NON-NLS-1$
		redoButton = new JButton(Util.getIconForKey("ToolboxPanel.REDO")); //$NON-NLS-1$ 
		lineTool.setBounds(new Rectangle(0,40,24,24));
		ellipseTool.setBounds(new Rectangle(24,40,24,24));
		pencilTool.setBounds(new Rectangle(0,64,24,24));
		rectTool.setBounds(new Rectangle(24,64,24,24));
		fillTool.setBounds(new Rectangle(0,88,24,24));
		eyeDropperTool.setBounds(new Rectangle(24,88,24,24));
		sprayTool.setBounds(new Rectangle(0,112,24,24));
		zoomTool.setBounds(new Rectangle(24,112,24,24));
		undoButton.setBounds(new Rectangle(0,136,24,24));
		redoButton.setBounds(new Rectangle(24,136,24,24));
		toolGroup.add(lineTool);
		toolGroup.add(ellipseTool);
		toolGroup.add(pencilTool);
		toolGroup.add(rectTool);
		toolGroup.add(fillTool);
		toolGroup.add(eyeDropperTool);
		toolGroup.add(sprayTool);
		toolGroup.add(zoomTool);
		lineTool.addActionListener(this);
		ellipseTool.addActionListener(this);
		pencilTool.addActionListener(this);
		rectTool.addActionListener(this);
		fillTool.addActionListener(this);
		eyeDropperTool.addActionListener(this);
		sprayTool.addActionListener(this);
		undoButton.addActionListener(this);
		redoButton.addActionListener(this);
		add(lineTool);
		add(ellipseTool);
		add(pencilTool);
		add(rectTool);
		add(fillTool);
		add(eyeDropperTool);
		add(sprayTool);
		add(zoomTool);
		add(undoButton);
		add(redoButton);

		fp = new FillPanel();
		fp.setBounds(0,166,48,72);
		add(fp);

		lp = new LinePanel();
		lp.setBounds(0,246,48,80);
		add(lp);
		}

	public void actionPerformed(ActionEvent e)
		{
		if (e.getSource() == undoButton)
			{
			EasyImageEditor.mainPanel.gui.canvas.undo();
			}
		if (e.getSource() == redoButton)
			{
			EasyImageEditor.mainPanel.gui.canvas.redo();
			}
		}

	public Tool getTool()
		{
		if (lineTool.isSelected()) return Tool.LINE;
		if (ellipseTool.isSelected()) return Tool.ELLIPSE;
		if (pencilTool.isSelected()) return Tool.PENCIL;
		if (rectTool.isSelected()) return Tool.RECT;
		if (fillTool.isSelected()) return Tool.FILL;
		if (eyeDropperTool.isSelected()) return Tool.EYEDROPPER;
		if (sprayTool.isSelected()) return Tool.SPRAY;
		if (zoomTool.isSelected()) return Tool.ZOOM;

		return null;
		}

	public enum Tool
		{
		LINE,ELLIPSE,PENCIL,RECT,FILL,EYEDROPPER,SPRAY,ZOOM;
		}
	}
